<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/7/3
     * Time: 下午12:20
     */

    namespace app\member\logic;


    use app\admin\model\Plugin;
    use app\common\utils\UtilEncryption;
    use app\face2market\model\Face2marketMemberTransactionModel;
    use app\member\model\MemberInfoModel;
    use app\member\model\MemberModel;
    use app\member\model\MemberReferrerModel;
    use app\payment\model\PaymentInfoModel;
    use app\wechat\model\MemberWechatModel;
    use app\payment\model\WalletModel;
    use app\base\model\WalletProductModel;
    use think\Cache;
    use think\Db;
    use think\Hook;

    class Member
    {

        /**注册
         *
         * @param        $phone
         * @param        $open_id
         * @param        $register_type
         * @param string $referrer_phone
         *
         * @return array
         */
        static public function miniapp_register($phone,$open_id,$register_type,$referrer_phone = '')
        {

            if (!in_array($register_type,['officialAccount','miniProgram','app']))
            {
                return ['error' => '注册类型错误，或不存在！'];
            }

            try
            {
                Db::startTrans();

                $member = MemberModel::create(['phone' => $phone]);


                if ($register_type == 'officialAccount')
                {
                    $data['officialAccount_open_id'] = $open_id;
                }
                if ($register_type == 'miniProgram')
                {
                    $data['miniProgram_open_id'] = $open_id;
                }
                if ($register_type == 'app')
                {
                    $data['app_open_id'] = $open_id;
                }
                $data['member_id'] = $member->id;

                MemberWeChatModel::create($data);
                if (!empty($referrer_phone))
                {
                    MemberReferrerModel::referrer($member->id,$referrer_phone);
                }

                $member_register = ['member_id' => $member->id];
                MemberInfoModel::initmember($member->id);

                WalletModel::initmember($member->id);
                WalletProductModel::CheckUserProductNum($member->id);
                //                Hook::listen('member_register',$member_register);
                Db::commit();

                return ['success' => '注册成功！'];
            }
            catch (\Exception $exception)
            {
                Db::rollback();
                return ['error' => '注册失败！'];// . $member . '+' . json_encode($MemberWeChatModel, true) . '+' . json_encode($MemberReferrerModel, true) . '+' . json_encode($MemberInfoModel, true) . '+' . json_encode($WalletModel, true)];
            }
        }

        /**注册
         *
         * @param $phone
         * @param $password
         * @param $referrer_phone
         *
         * @return array
         */
        static public function app_register($phone,$password,$referrer_phone = 0,$data=[])
        {

            if ($referrer_phone)
            {
                $referrer_member = MemberModel::where('phone',$referrer_phone)->find();
            }
            else
            {
                $referrer_member['id']   = 0;
                $referrer_member['puid'] = 0;
            }

            $Member_data['phone_puid'] = $referrer_member['id'];
            $Member_data['puid']       = $referrer_member['puid'];
            $Member_data['phone']      = $phone;
            $Member_data['password']   = UtilEncryption::encryptMd5($password,config('member_pwd_key'));
            try
            {
                Db::startTrans();
                $member          = MemberModel::create($Member_data);
                $member_register = ['member_id' => $member->id];
                MemberInfoModel::initmember($member->id);
                //                Face2marketMemberTransactionModel::create_t($member->id);
                PaymentInfoModel::create(['member_id' => $member->id]);
                WalletModel::initmember($member->id);
                WalletProductModel::CheckUserProductNum($member->id);

                //有推荐人关系
//                if ($referrer_member['id'])
//                {
//                    $data['member_id'] = $member->id;
//                    hook('member_register',$data);
//                }


                /**
                 * 测试数据
                 */
                Db::commit();

                return ['success' => '注册成功！','member_id' => $member->id];
            }
            catch (\Exception $exception)
            {
                Db::rollback();
                return ['error' => '注册失败！'];// . $member . '+' . json_encode($MemberWeChatModel, true) . '+' . json_encode($MemberReferrerModel, true) . '+' . json_encode($MemberInfoModel, true) . '+' . json_encode($WalletModel, true)];
            }
        }

        static public function reset_password($phone,$password)
        {

            $password = UtilEncryption::encryptMd5($password,config('member_pwd_key'));
            return MemberModel::update(['password' => $password],['phone' => $phone,]);

        }

        /**短信发送
         *
         * @param      $data
         *
         * @return array
         */
        static public function sendPhoneCode($data)
        {

            $AliyunSms = Plugin::where(['name' => 'TencentSms','status' => 1])->find();

            if ($AliyunSms)
            {
                $phoneCode = UtilEncryption::random(6,1);

                Cache::set('timephone_' . $data['phone'],$phoneCode,300);

                if (config('develop_mode'))
                {
                    $requset = $phoneCode;
                }
                else
                {
                    $requset = plugin_action('TencentSms','TencentSms','VerifiCode',[
                        'phoneNum'  => $data['phone'],
                        'phoneCode' => $phoneCode
                    ]);
                }

                if ($requset['code'] == 200)
                {
                    $msg = [
                        'type' => 'success',
                        'msg'  => '短信发送成功！'
                    ];

                }
                else
                {
                    $msg = [
                        'type' => 'error',
                        'msg'  => '短信发送失败！'
                    ];

                }
            }
            else
            {
                $msg = [
                    'type' => 'error',
                    'msg'  => '短信插件未安装！'
                ];

            }
            if (config('develop_mode'))
            {
                $msg['msg'] = $msg['msg'] . '|' . $phoneCode;
            }
            return $msg;

        }
    }